//
//  RadioDetailsInfoView.m
//  aMoment
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "RadioDetailsInfoView.h"
#import "AllHeaderFiles.h"

@implementation RadioDetailsInfoView

- (void)dealloc
{
    [_tableView release];
    [_dataSourceArray release];
    
    [_scrollView release];
    [_coverImageView release];
    [_titleLabel release];
    [_downImageView release];
    [_musicSlider release];
    [_musicTime release];
    [_prevImageView release];
    [_nextImageView release];
    [_playImageView release];
    
    [_webView release];
    
    [_detailsInfo release];
    [_listArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self.dataSourceArray = [NSArray array];
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)createViews
{
    //播放界面
    self.scrollView = [[UIScrollView alloc] init];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollView];
    [_scrollView release];
    
    self.coverImageView = [[UIImageView alloc] init];
    _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    _coverImageView.clipsToBounds = YES;
    [_scrollView addSubview:_coverImageView];
    [_coverImageView release];
    
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [_scrollView addSubview:_titleLabel];
    [_titleLabel release];
    
//    self.downImageView = [[UIImageView alloc] init];
//    _downImageView.image = [UIImage imageNamed:@"iconfont-xiazai"];
//    [_scrollView addSubview:_downImageView];
//    [_downImageView release];
    
//    self.musicSlider = [[UISlider alloc] init];
//    [_scrollView addSubview:_musicSlider];
//    [_musicSlider release];
    
//    self.musicTime = [[UILabel alloc] init];
//    _musicTime.backgroundColor = [UIColor grayColor];
//    [_scrollView addSubview:_musicTime];
//    [_musicTime release];
    
    NSArray *imageArray = [NSArray arrayWithObjects:@"desk2_pre", @"desk2_pause", @"desk2_next", nil];
    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageArray[i]];
        imageView.tag = 1000 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(musicAction:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        [tap release];
        [_scrollView addSubview:imageView];
        if (i == 0) {
            _prevImageView = imageView;
        }
        else if (i == 1) {
            _playImageView = imageView;
        }
        else {
            _nextImageView = imageView;
        }
        [imageView release];
    }
    
    //列表界面
    self.tableView = [[UITableView alloc] init];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [self createHeaderView];
    _tableView.rowHeight = 55.0;
    [_scrollView addSubview:_tableView];
    [_tableView release];
    
    //webView界面
    self.webView = [[UIWebView alloc] init];
    _webView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_webView];
    [_webView release];
}

#pragma mark 创建headerView
- (UILabel *)createHeaderView
{
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    headerLabel.text = @"播放列表";
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    headerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    return headerLabel;
}

#pragma mark 点击播放按钮, 上下曲按钮
- (void)musicAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1000) {
        if (_index == 0) {
            _index = _listArray.count - 1;
        } else {
            _index -= 1;
        }
        self.block([_dataSourceArray[_index] objectForKey:@"tingid"]);
    }
    if (tap.view.tag == 1002) {
        if (_index == _listArray.count - 1) {
            _index = 0;
        } else {
            _index += 1;
        }
        self.block([_dataSourceArray[_index] objectForKey:@"tingid"]);
    }
    [self playMusic];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.Height);
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, _scrollView.Height);
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    
    _coverImageView.frame = CGRectMake(SCREEN_WIDTH + 50 * FIT_WIDTH, 50 * FIT_WIDTH, SCREEN_WIDTH - 100 * FIT_WIDTH, SCREEN_WIDTH - 100 * FIT_WIDTH);
    _titleLabel.frame = CGRectMake(_coverImageView.X, _coverImageView.Y + _coverImageView.Height + 30 * FIT_HEIGHT, _coverImageView.Width, 20 * FIT_HEIGHT);
//    _musicSlider.frame = CGRectMake(_titleLabel.X + 40, _titleLabel.Y + _titleLabel.Height + 70, _titleLabel.Width - 80, 25);
//    _downImageView.frame = CGRectMake(_titleLabel.X - 12.5, _musicSlider.Y, 25, 25);
//    _musicTime.frame = CGRectMake(_titleLabel.X + _titleLabel.Width - 15, _musicSlider.Y, 50, 25);
    _prevImageView.frame = CGRectMake(_titleLabel.X, self.Height - 35 * FIT_HEIGHT - 48 * FIT_WIDTH, 48 * FIT_WIDTH, 48 * FIT_WIDTH);
    _nextImageView.frame = CGRectMake(_titleLabel.X + _titleLabel.Width - 48 * FIT_WIDTH, _prevImageView.Y, _prevImageView.Width, _prevImageView.Height);
    _playImageView.frame = CGRectMake(SCREEN_WIDTH + self.Width / 2 - 32 * FIT_WIDTH, self.Height - 27 * FIT_HEIGHT - 64 * FIT_WIDTH, 64 * FIT_WIDTH, 64 * FIT_WIDTH);
    
    //下载界面
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.Height);
    
    //webView界面
    _webView.frame = CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, self.Height);
}

#pragma mark 重写detailsInfo
- (void)setDetailsInfo:(RadioDetailsInfo *)detailsInfo
{
    if (_detailsInfo != detailsInfo) {
        [_detailsInfo release];
        _detailsInfo = [detailsInfo retain];
    }//placeholder-cd
    [_coverImageView setImageWithURL:[NSURL URLWithString:[_detailsInfo.playInfo objectForKey:@"imgUrl"]] placeholderImage:[UIImage imageNamed:@"placeholder-cd"]];
    [self.delegate passImage:_coverImageView.image];
    
    _titleLabel.text = [_detailsInfo.playInfo objectForKey:@"title"];
    [_tableView reloadData];
    
    //网页详情
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_detailsInfo.playInfo objectForKey:@"webview_url"]]];
    [_webView loadRequest:request];
}

#pragma mark 重写listArray
- (void)setListArray:(NSArray *)listArray
{
    if (_listArray != listArray) {
        [_listArray release];
        _listArray = [listArray retain];
    }
    _dataSourceArray = _listArray;
    [_tableView reloadData];
}

#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MusicListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[MusicListTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.songNameLabel.text = [NSString stringWithFormat:@"%ld. %@", indexPath.row + 1, [_dataSourceArray[indexPath.row] objectForKey:@"title"]];
    
    NSString *singerName = [NSString string];
    singerName = [[[_dataSourceArray[indexPath.row] objectForKey:@"playInfo"] objectForKey:@"authorinfo"] objectForKey:@"uname"];
    if (singerName == nil) {
        singerName = [[_dataSourceArray[indexPath.row] objectForKey:@"userinfo"] objectForKey:@"uname"];
    }
    cell.singerLabel.text = [NSString stringWithFormat:@"by: %@", singerName];
    
    //
//    void (^downBlock)(float) = ^(float progress) {
//        NSLog(@"%.2f%%", progress);
//        NSLog(@"%@", self.superview);
//    };
//    cell.block = downBlock;
//    cell.musicInfoArray = [NSArray arrayWithObjects:[_dataSourceArray[_index] objectForKey:@"musicUrl"], [NSString stringWithFormat:@"%@ - %@.mp3", singerName, [_dataSourceArray[indexPath.row] objectForKey:@"title"]], nil];
    return cell;
}

#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.block([_dataSourceArray[indexPath.row] objectForKey:@"tingid"]);
    _index = indexPath.row;
    [self playMusic];
    [_tableView reloadData];
}

#pragma mark 播放音乐
- (void)playMusic
{
    Music *music = [[Music alloc] init];
    
    if (![_dataSourceArray[_index] objectForKey:@"imgUrl"]) {
        RadioDetails *radioDerails = [[RadioDetails alloc] init];
        [radioDerails setValuesForKeysWithDictionary:self.dataSourceArray[_index]];
        music.musicURL = radioDerails.musicUrl;
        music.coverImageURL = radioDerails.coverimg;
        music.songName = radioDerails.title;
        music.singer = [[radioDerails.playInfo objectForKey:@"userinfo"] objectForKey:@"uname"];
        music.tingId = radioDerails.tingid;
    }
    else {
        music.musicURL = [_dataSourceArray[_index] objectForKey:@"musicUrl"];
        music.coverImageURL = [_dataSourceArray[_index] objectForKey:@"imgUrl"];
        music.songName = [_dataSourceArray[_index] objectForKey:@"title"];
        music.singer = [[_dataSourceArray[_index] objectForKey:@"userinfo"] objectForKey:@"uname"];
        music.tingId = [_dataSourceArray[_index] objectForKey:@"tingid"];
    }
    
    //播放音乐
    [[MusicSingleton shareInstance] playMusicWith:music.musicURL];
    
    //改变播放按钮图片
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"rate"]) {
        _playImageView.image = [UIImage imageNamed:@"desk2_pause"];
    } else {
        _playImageView.image = [UIImage imageNamed:@"desk2_play"];
    }
    
    //改变音乐播放条详情
    [[MusicSingleton shareInstance] setMusicModel:music];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < SCREEN_WIDTH) {
        [_tableView reloadData];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
